namespace Profit
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media.Imaging;
    using Core.Contracts.Enums;
    using Core.Contracts.Interfaces;

    /// <summary>
    /// Helper class
    /// </summary>
    public static class Helpers
    {
        /// <summary>
        /// Loads an embedded Image
        /// </summary>
        /// <param name="name">The name of the image file, with the extension, located in the Icons directory.</param>
        /// <returns>A BitmapImage or Null</returns>
        public static BitmapImage LoadIcon(string name)
        {
            try
            {
                var bi = new BitmapImage();
                bi.BeginInit();
                bi.UriSource = new Uri(String.Format("pack://application:,,/Resources/Icons/{0}", name), UriKind.Absolute);
                bi.EndInit();
                return bi;
            }
            catch (Exception)
            {
                return null;
            }
        }
        /// <summary>
        /// Loads an embedded Cursor
        /// </summary>
        /// <param name="name">The name of the image file, with the extension, located in the Icons directory.</param>
        /// <returns>A BitmapImage or Null</returns>
        public static Cursor LoadCursor(string name)
        {
            try
            {
                var res = Application.GetResourceStream(new Uri(String.Format("pack://application:,,/Resources/Cursors/{0}", name)));
                return res == null ? Cursors.Arrow : new Cursor(res.Stream);
            }
            catch (Exception)
            {
                return Cursors.Arrow;
            }
        }
        /// <summary>
        /// Loads the module group cursor used for drag and drop operation
        /// </summary>
        /// <param name="group"></param>
        /// <returns></returns>
        public static Cursor LoadModuleGroupCursor(ModuleGroupsEnum group)
        {
            return LoadCursor( string.Format("{0}.cur", group));
        }
        /// <summary>
        /// Parse the content of the history extension file from the game
        /// </summary>
        /// <param name="file">The complet path to the file</param>
        /// <param name="agent">An IAgent instance</param>
        public static void ParseExtensionHistoryFile(string file, IAgent agent)
        {
            if (agent == null || !File.Exists(file)) return;

            var dico = new Dictionary<string, ushort>();

            using (var sr = File.OpenText(file))
            {
                // header line, ignore it
                sr.ReadLine();

                while (!sr.EndOfStream)
                {
                    var line = sr.ReadLine();
                    if (string.IsNullOrEmpty(line)) continue;

                    var parts = line.Replace("\"", "").Split(',');

                    string extension = parts[0].Replace("Rapid-firing", "Rapid firing");
                    if (dico.ContainsKey(extension)) continue;

                    ushort level = 0;
                    if(!ushort.TryParse(parts[1], out level)) continue;
                    
                    dico.Add(extension, level);
                }
            }

            int count = 0;
            foreach (var ext in agent.Extensions.Where(ext => dico.ContainsKey(ext.Name)))
            {
                ext.Level = dico[ext.Name];
                count++;
            }

            App.Trace.TraceInformation(string.Format("{0}/{1} extensions updated", count, dico.Count));
        }
    }
}